﻿using RFID.Model;
using RFID.VModel;
using System;
using System.Collections.Generic;
using System.Linq;

namespace RFID.BLL
{
    public class CardInfoBLL
    {
        /// <summary>
        /// 验证一卡通
        /// </summary>
        /// <param name="card">一卡通</param>
        /// <returns></returns>
        public static OperateResult<bool> Validate(CardInfo card)
        {
            if (card == null)
            {
                return OperateResult<bool>.Failed("一卡通尚未开通！");
            }
            else if (card.IsDelete)
            {
                return OperateResult<bool>.Failed("一卡通已经被注销！");
            }
            else if (card.Disabled)
            {
                return OperateResult<bool>.Failed("一卡通已经被锁定！");
            }
            return OperateResult<bool>.Succeeded(true);
        }

        /// <summary>
        /// 单张一卡通
        /// </summary>
        /// <param name="cid">学号</param>
        /// <param name="pwd">密码</param>
        /// <returns>一卡通或 null</returns>
        public static OperateResult<CardInfo> Login(string sid, string pwd)
        {
            using (RFIDDataContext db = new RFIDDataContext())
            {
                var student = db.StuInfo.FirstOrDefault(p => p.Id.ToString() == sid && p.Pwd == pwd);
                if (student == null)
                {
                    return OperateResult<CardInfo>.Failed("学号或密码不正确！");
                }

                var card = student.CardInfo.FirstOrDefault();
                if (card == null)
                {
                    return OperateResult<CardInfo>.Failed("您尚未开通一卡通服务！");
                }
                return card.IsDelete ? OperateResult<CardInfo>.Failed("绑定的一卡通已经被注销！") : OperateResult<CardInfo>.Succeeded(card);
            }
        }

        /// <summary>
        /// 查询一卡通
        /// </summary>
        /// <param name="key">卡号或学号</param>
        /// <returns>一卡通列表</returns>
        public static List<CardInfoView> Select(string key = "")
        {
            using (RFIDDataContext db = new RFIDDataContext())
            {
                return db.CardInfo.Where(p => p.IsDelete == false && (string.IsNullOrEmpty(key) || p.Id == key || p.Sid.ToString() == key)).Select(p => new CardInfoView { 卡号 = p.Id, 余额 = p.Rich, 持卡人 = p.StuInfo.Name, 状态 = p.Disabled ? "锁定" : "正常" }).ToList();
            }
        }

        /// <summary>
        /// 注册一卡通
        /// </summary>
        /// <param name="card">一卡通</param>
        /// <returns></returns>
        public static OperateResult<bool> AddCard(CardInfo card)
        {
            using (RFIDDataContext db = new RFIDDataContext())
            {
                var temp = db.CardInfo.FirstOrDefault(p => p.Id == card.Id);
                if (temp != null)
                {
                    return OperateResult<bool>.Failed("一卡通已经被占用！");
                }
                var temp2 = db.CardInfo.FirstOrDefault(p => p.Sid == card.Sid);
                if (temp2 != null)
                {
                    return OperateResult<bool>.Failed($"该学员已经绑定过卡{{temp2.Id}}！");
                }
                try
                {
                    if (card.Rich > 0)
                    {
                        card.RichInfo.Add(new RichInfo() { Date = DateTime.Now, Rich = card.Rich });
                    }
                    db.CardInfo.InsertOnSubmit(card);
                    db.SubmitChanges();
                    return OperateResult<bool>.Succeeded(true);
                }
                catch (Exception ex)
                {
                    return OperateResult<bool>.Failed(ex);
                }
            }
        }

        /// <summary>
        /// 修改密码（卡号、密码）
        /// </summary>
        /// <param name="card">一卡通</param>
        /// <param name="newPwd">新密码</param>
        /// <returns></returns>
        public static OperateResult<bool> ChangePwd(CardInfo card, string newPwd)
        {
            using (RFIDDataContext db = new RFIDDataContext())
            {
                var temp = db.CardInfo.FirstOrDefault(p => p.Id == card.Id && p.Pwd == card.Pwd);
                if (temp != null)
                {
                    try
                    {
                        temp.Pwd = newPwd;
                        db.SubmitChanges();
                        return OperateResult<bool>.Succeeded(true);
                    }
                    catch (Exception ex)
                    {
                        return OperateResult<bool>.Failed(ex);
                    }
                }
                return OperateResult<bool>.Failed("一卡通卡号或原密码不正确！");
            }
        }

        /// <summary>
        /// 找回密码（卡号、学号）
        /// </summary>
        /// <param name="card">一卡通</param>
        /// <param name="newPwd">新密码</param>
        /// <returns></returns>
        public static OperateResult<bool> LostPwd(CardInfo card, string newPwd)
        {
            using (RFIDDataContext db = new RFIDDataContext())
            {
                var temp = db.CardInfo.FirstOrDefault(p => p.Id == card.Id && p.Sid == card.Sid);
                if (temp != null)
                {
                    try
                    {
                        temp.Pwd = newPwd;
                        db.SubmitChanges();
                        return OperateResult<bool>.Succeeded(true);
                    }
                    catch (Exception ex)
                    {
                        return OperateResult<bool>.Failed(ex);
                    }
                }
                return OperateResult<bool>.Failed("一卡通卡号或学号不正确！");
            }
        }

        /// <summary>
        /// 补办一卡通（学号、密码）
        /// </summary>
        /// <param name="card">旧一卡通</param>
        /// <param name="newId">新一卡通卡号</param>
        /// <returns></returns>
        public static OperateResult<bool> ChangeCard(CardInfo card, string newId)
        {
            using (RFIDDataContext db = new RFIDDataContext())
            {
                if (db.CardInfo.FirstOrDefault(p => p.Id == newId) != null)
                    return OperateResult<bool>.Failed("新一卡通已经被占用！");

                var temp = db.CardInfo.FirstOrDefault(p => p.Sid == card.Sid && p.Pwd == card.Pwd);
                if (temp != null)
                {
                    try
                    {
                        db.ChangeCard(temp.Id, newId);
                        return OperateResult<bool>.Succeeded(true);
                    }
                    catch (Exception ex)
                    {
                        return OperateResult<bool>.Failed(ex);
                    }
                }
                return OperateResult<bool>.Failed("一卡通卡号或原密码不正确！");
            }
        }
        /// <summary>
        /// 挂失一卡通（卡号、密码）
        /// </summary>
        /// <param name="card">一卡通</param>
        /// <returns></returns>
        public static OperateResult<bool> LostCard(CardInfo card)
        {
            using (RFIDDataContext db = new RFIDDataContext())
            {
                var temp = db.CardInfo.FirstOrDefault(p => p.Sid == card.Sid && p.Pwd == card.Pwd);
                if (temp != null)
                {
                    try
                    {
                        temp.Disabled = true;
                        db.SubmitChanges();
                        return OperateResult<bool>.Succeeded(true);
                    }
                    catch (Exception ex)
                    {
                        return OperateResult<bool>.Failed(ex);
                    }
                }
                return OperateResult<bool>.Failed("一卡通卡号或密码不正确！");
            }
        }

        /// <summary>
        /// 激活一卡通（卡号、密码）
        /// </summary>
        /// <param name="card">一卡通</param>
        /// <returns></returns>
        public static OperateResult<bool> ActiveCard(CardInfo card)
        {
            using (RFIDDataContext db = new RFIDDataContext())
            {
                var temp = db.CardInfo.FirstOrDefault(p => p.Id == card.Id && p.Pwd == card.Pwd);
                if (temp != null)
                {
                    try
                    {
                        temp.Disabled = false;
                        db.SubmitChanges();
                        return OperateResult<bool>.Succeeded(true);
                    }
                    catch (Exception ex)
                    {
                        return OperateResult<bool>.Failed(ex);
                    }
                }
                return OperateResult<bool>.Failed("一卡通卡号或密码不正确！");
            }
        }

        /// <summary>
        /// 注销一卡通（卡号、密码）
        /// </summary>
        /// <param name="card">一卡通</param>
        /// <returns></returns>
        public static OperateResult<bool> DestoryCard(CardInfo card)
        {
            using (RFIDDataContext db = new RFIDDataContext())
            {
                var temp = db.CardInfo.FirstOrDefault(p => p.Id == card.Id && p.Pwd == card.Pwd);
                if (temp != null)
                {
                    try
                    {
                        temp.IsDelete = true;
                        db.SubmitChanges();
                        return OperateResult<bool>.Succeeded(true);
                    }
                    catch (Exception ex)
                    {
                        return OperateResult<bool>.Failed(ex);
                    }
                }
                return OperateResult<bool>.Failed("一卡通卡号或密码不正确！");
            }
        }
    }
}
